<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:replace="_header_include::frag(~{::title},~{},~{})">
    <title>后台管理系统</title>
</head>
<body>
<div id="maindiv" class="maindiv mainPadding" style="display: none;">    
    <el-breadcrumb separator="/" class="elBreadcrumb marginB20">
        <el-breadcrumb-item>Home</el-breadcrumb-item>
        <el-breadcrumb-item>数据报表</el-breadcrumb-item>
        <el-breadcrumb-item>商务累计未收齐尾款表</el-breadcrumb-item>
    </el-breadcrumb>    
    <el-row class="marginB20">
         <shiro:hasPermission name="statistics:businessTotalArrearsTable:export">
        <el-button type="danger" @click="exportRs">导出Excel报表</el-button>
         </shiro:hasPermission>
        <el-button type="primary" style="float: right;" @click="goback()">返回</el-button>    
    </el-row>
    <div class="mainSearchBg">        
        <div class="mainSearchFormBox">
           <el-form :inline="true" :model="form" class="demo-form-inline mainSearchForm" ref="form">                 
                <el-form-item label="统计截止日期：">
                    <el-date-picker format="yyyy-MM-dd HH:mm" v-model="form.endTime" type="datetime" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="签约项目：">
                    <el-select v-model="form.projectId" placeholder="全部" style="width: 100%;" clearable filterable>
                        <el-option
                            v-for="item in projectArr"
                            :key="item.id"
                            :label="item.projectName"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="商务组：">  
                    <el-select v-model="form.orgId" placeholder="全部" style="width: 100%;" clearable filterable  @change="currentTelSale" :disabled="isDisabled">
                        <el-option
                            v-for="item in orgTeleArr"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="商务经理：">  
                    <el-select v-model="form.userId" placeholder="全部" style="width: 100%;" clearable filterable :disabled="isDisabledUserId">
                        <el-option
                            v-for="item in busSaleArr"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="大于等于：">
                    <el-input @keyup.native="number" maxlength="3" v-model="form.days" placeholder="输入查询天数" class="input-with-select"></el-input>
					<span>&nbsp;天</span> 
                </el-form-item>
                <div class="mainSearchBtnBox">
                    <el-button class="searchBtn" icon="el-icon-search" type="primary" @click="searchTable(1)" :disabled="loading">搜索</el-button>
                </div>  
            </el-form>
        </div>
        <el-row>
            <el-table 
                ref="tableData"
                tooltip-effect="dark"
                style="width: 1100px;"
                border
                :data="tableData"
                v-loading="loading"
                :cell-style="centerStyle"
                :header-cell-style="centerStyle"
                :span-method="arraySpanMethod">
                <el-table-column type="index" label="序号" align="center" width="55" :index="indexFun"></el-table-column>
                <el-table-column prop="projectName" label="项目"></el-table-column>
                <el-table-column prop="owedDays" label="欠款天数">
                    <template slot-scope="scope">
                        <span v-if="scope.row.owedDays!='合计'">{{scope.row.owedDays}}</span>
                        <span v-else>{{scope.row.owedDays}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="userName" label="商务经理"></el-table-column>
                <el-table-column prop="noCreditNum" label="未收齐尾款笔数"></el-table-column>
                <el-table-column prop="noCreditAmount" label="未收齐尾款金额"></el-table-column>
            </el-table>
            <table-pagination :pager="pager" @change="searchTable()" :papernum="[20,50,100]"></table-pagination>
        </el-row>
    </div>
</div>
<!-- import common js-->
<div th:include="_footer_include :: #jsLib"></div>
</body>
<style>
    .el-table tr:nth-of-type(1) td:nth-of-type(n+1) {font-weight: bold;}
    .mainSearchFormBox form .el-form-item:first-child .el-form-item__label{
        width: 100px;
    }
</style>
<script th:inline="javascript">
    var parentParam=[[${unpaidFundsQueryDto}]];
    var projectList = [[${projectList}]];
    var orgIdList=[[${teleGroupList}]];
    var curOrgId = [[${curOrgId}]];
    var curUserId = [[${curUserId}]];   
    var mainDivVM = new Vue({
        el: '#maindiv',
        data: function() {
            return {
                loading: false,
                isDisabled: false,
                isDisabledUserId: false,
                pager:{
                    total: 0,
                    currentPage: 1,
                    pageSize: 20,
                },
                form: {
                    endTime:'',
                    orgId: '',
                    userId: '',
                    projectId: '',
                    days: ''
                },
                export: {
                    endTime:'',
                    orgId: '',
                    userId: '',
                    projectId: '',
                    days: ''
                },
                tableData: [
                    // {   owedDays: 99999,
                    //     noCreditNum: 1,
                    //     noCreditAmount: 1,
                    //     projectName: 1,
                    //     userId: 1,
                    //     userName: 1,
                    //     orgId: 1,
                    // },
                    // {   owedDays: 2,
                    //     noCreditNum: 2,
                    //     noCreditAmount: 2,
                    //     projectName: 2,
                    //     userId: 2,
                    //     userName: 2,
                    //     orgId: 2,
                    // },
                ],
                orgTeleArr: orgIdList, // 商务组
                busSaleArr: '', // 商务经理
                projectArr: projectList, // 来访项目
            }             
        },
        methods: {  
            number(){
        　　　 	 this.form.days=this.form.days.replace(/[^\.\d]/g,'');
                this.form.days=this.form.days.replace('.','');
        　　},  
            centerStyle(){
                return "text-align:center"
            },  
            firstCurTelSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.getTelSale(seleVal);
            },
            currentTelSale(seleVal){
                if(seleVal==null||seleVal==""){
                    return ;
                }
                this.form.userId = '';
                this.getTelSale(seleVal);
            },
            getTelSale(seleVal){
                param={};
                param.orgId=seleVal;
                param.roleCode="SWJL";
                axios.post('/base/getSaleList' ,param).then(function (response) {  
                    mainDivVM.busSaleArr=response.data.data;
                }).catch(function (error) {
                        mainDivVM.$message({
                        message: "商务经理请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            searchTable(firstPage) { 
                var endTime = this.form.endTime;
                if(endTime){
                    endTime = new Date(this.form.endTime);
                    // var date = new Date();
                    // var currentDate = moment(date).format("YYYY-MM-DD 23:59");
                    // var currentDateRes = new Date(currentDate);
                    // if(endTime > currentDateRes){
                    //     this.$message({
                    //         message: '不能超过当天时间',
                    //         type: 'warning'
                    //     });
                    //     return;
                    // }
                    endTime = this.limitTime(null, endTime);
                }else{
                    this.$message({
                        message: '请选择截止时间',
                        type: 'warning'
                    });
                    return;
                }   
    
                var param = {};
                param.endTime = this.minuteFormat(endTime);
                param.pageSize = this.pager.pageSize;
    
                if(firstPage){
                    param.pageNum = firstPage;
                }else{
                    param.pageNum = this.pager.currentPage;
                }
                param.orgId = this.form.orgId;
                param.userId = this.form.userId;
                param.projectId = this.form.projectId;
                param.days = this.form.days;
                this.loading = true; 
                axios.post('/statistics/unpaidFunds/getDirectorTwoPageList',param).then(function (response) {
                    if(!response || !response.data){
                        mainDivVM.$message({
                            message: "接口调用失败",
                            type: 'error'
                        });
                        return ;
                    }
                    var resobj= response.data;
                    if(resobj.code != '0'){
                        mainDivVM.loading = false;
                        mainDivVM.$message({
                            message: resobj.msg,
                            type: 'error'
                        });
                        return ;
                    }else if(resobj.data && resobj.data.tableData){
                        var resobjData = resobj.data;    
                        var tableData = resobjData.tableData;
                        mainDivVM.tableData = tableData.data;
                        if( tableData.data.length && resobj.data.totalData ){
                            var totalData = resobjData.totalData[0];
                            mainDivVM.tableData.unshift(totalData);
                        }
                        mainDivVM.pager.currentPage = tableData.currentPage;
                        mainDivVM.pager.total = tableData.total;
                        mainDivVM.loading = false;
                        mainDivVM.export.endTime = mainDivVM.minuteFormat(endTime);
                        mainDivVM.export.orgId = mainDivVM.form.orgId;
                        mainDivVM.export.userId = mainDivVM.form.userId;
                        mainDivVM.export.projectId = mainDivVM.form.projectId;
                        mainDivVM.export.days = mainDivVM.form.days;
                    }else{
                        mainDivVM.loading = false;
                    }
                }).catch(function (error) {
                    mainDivVM.loading = false;
                    mainDivVM.$message({
                        message: "请求失败，请稍后重试",
                        type: "error"
                    });
                });
            },
            limitTime (start, end) {
                var endTime = this.minuteFormat (end);
                var startTime = this.minuteFormat (start);
                var date = new Date();
                date = this.minuteFormat (date);// 现在的时间
                date = date.slice(0,8);
                if(startTime){
                    var s = startTime.slice(0,8);
                    if(s < date){
                        this.form.startTime = moment(start).format("YYYY-MM-DD 00:00");
                        return this.form.startTime;
                    }
                    return start;
                }
                if(endTime){
                    var e = endTime.slice(0,8);
                    if(e < date){
                        this.form.endTime = moment(end).format("YYYY-MM-DD 23:59");
                        return this.form.endTime;                        
                    }
                    return end;
                }
            },
            goback () {
                window.history.go(-1);
            },
            arraySpanMethod({ row, column, rowIndex, columnIndex }) { // 合并第一行
                if (rowIndex === 0) {
                    if (columnIndex === 0) {
                        return [0, 0];
                    } else if (columnIndex === 1) {
                        return [0, 0];
                    }else if (columnIndex === 2) {
                        return [1, 4];
                    }else if (columnIndex === 3) {
                        return [0, 0];
                    }
                }
            },
            indexFun (index) { // 表格序号
                return index;
            },
            dateFormat (row, column) { 
                var date = row[column.property];
                if (!date) {return "";}
                var d = date.toString();
                var oYear = d.slice(0,4);
                var oMonth = d.slice(4,6);
                var oDay = d.slice(6);
                return oYear+"-"+oMonth+"-"+oDay; 
            },
            minuteFormat (m) {
                if (!m) {return "";}
                var timeStr = new Date(m);
                var yearRes = timeStr.getFullYear()
                var monthRea = timeStr.getMonth()+1;
                monthRea = monthRea < 10 ?  "0" + monthRea : monthRea;
                var dateRes = timeStr.getDate();
                dateRes = dateRes < 10 ?  "0" + dateRes : dateRes;  
                var hoursRes = timeStr.getHours();
                hoursRes = hoursRes < 10 ?  "0" + hoursRes : hoursRes;  
                var minRes = timeStr.getMinutes();
                minRes = minRes < 10 ?  "0" + minRes : minRes;                
                var timeRes = "" + yearRes + monthRea + dateRes + hoursRes + minRes;
                return timeRes;
            },
            dateTimeFormat (dt) { 
                if (!dt) {return "";}
                var d = dt.toString();
                var oYear = d.slice(0,4);
                var oMonth = d.slice(4,6);
                var oDay = d.slice(6,8);
                var oHour = d.slice(8,10);
                var oMinute = d.slice(10);
                return oYear+"-"+oMonth+"-"+oDay+" "+oHour+":"+oMinute;
            },
            exportRs(){
                var param = {};
                param.endTime = this.export.endTime;
                param.orgId = this.export.orgId;
                param.userId = this.export.userId;
                param.projectId = this.export.projectId;
                param.days = this.export.days;
                axios.post('/statistics/unpaidFunds/exportDirectorTwoList',param,{responseType:'blob'})
                .then(function (response) {
                    var data =  response.data;
                    var fileName = response.headers.filename;
                    saveAs(data,decodeURI(fileName));
                })
                .catch(function (error) {
                    mainDivVM.$message({
                        message: "导出失败，请稍后重试",
                        type: "error"
                    });
                });
            }
        },
        created(){ 
            var today = new Date();
            var endTime = moment(today).format("YYYY-MM-DD 23:59");
            if(curOrgId){ // 有角色
                this.isDisabled = true;
                this.form.orgId = curOrgId;
                if(curUserId){
                    this.isDisabledUserId = true;
                    this.form.userId = curUserId;
                }else{
                    this.form.userId = parentParam&&parentParam.userId?parentParam.userId:'';
                }
            }else{
                this.form.orgId = parentParam&&parentParam.orgId ? parentParam.orgId:'';
                this.form.userId = parentParam&&parentParam.userId ? parentParam.userId:'';
            }
            if(parentParam && parentParam.endTime){
                this.form.endTime = this.dateTimeFormat(parentParam.endTime);
                this.export.endTime = parentParam.endTime;
            }else{
                this.form.endTime = endTime;
                this.export.endTime = this.minuteFormat(this.form.endTime);
            }
            this.form.projectId = parentParam&&parentParam.projectId ? parentParam.projectId:''; 
            this.form.days = parentParam&&parentParam.days ? parentParam.days:''; 
            this.export.orgId = this.form.orgId;
            this.export.userId = this.form.userId; 
            this.export.projectId = this.form.projectId; 
            this.export.days = this.form.days; 
            
            var localVal=localStorage.getItem('allChangePageSize')?parseInt(localStorage.getItem('allChangePageSize')):'';
            if(localVal){this.pager.pageSize = localVal;}

            this.firstCurTelSale(this.export.orgId); 
            this.searchTable();
        },
        mounted(){
            document.getElementById('maindiv').style.display = 'block';
        }
    })
    
</script>
</html>